package cn.t.repository.impl.order;

import cn.t.converter.order.ReplenishmentConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.order.ReplenishmentDO;
import cn.t.persistence.entity.order.ReplenishmentPO;
import cn.t.persistence.mapper.order.ReplenishmentMapper;
import cn.t.repository.order.ReplenishmentRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 补货订单表(Replenishment)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:10
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ReplenishmentRepositoryImpl extends RepositoryImpl<ReplenishmentMapper, ReplenishmentPO, ReplenishmentConverter, ReplenishmentDO> implements ReplenishmentRepository {


    @Override
    public List<ReplenishmentDO> findByModel(ReplenishmentDO replenishmentDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<ReplenishmentPO> list = getWrapper(replenishmentDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<ReplenishmentPO> getWrapper(ReplenishmentDO replenishmentDO) {
        LambdaQueryChainWrapper<ReplenishmentPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(replenishmentDO.getRsSn())) {
            wrapper.eq(ReplenishmentPO::getRsSn, replenishmentDO.getRsSn().trim());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getRsSnList())) {
            wrapper.in(ReplenishmentPO::getRsSn, replenishmentDO.getRsSnList());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getDeviceId())) {
            wrapper.eq(ReplenishmentPO::getDeviceId, replenishmentDO.getDeviceId());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getDeviceIds())) {
            wrapper.in(ReplenishmentPO::getDeviceId, replenishmentDO.getDeviceIds());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getRsStatus())) {
            wrapper.eq(ReplenishmentPO::getRsStatus, replenishmentDO.getRsStatus());
        }
        if (!CollectionUtils.isEmpty(replenishmentDO.getRsStatusList())) {
            wrapper.in(ReplenishmentPO::getRsStatus, replenishmentDO.getRsStatusList());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getUserId())) {
            wrapper.eq(ReplenishmentPO::getUserId, replenishmentDO.getUserId());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getUserName())) {
            wrapper.like(ReplenishmentPO::getUserName, replenishmentDO.getUserName());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getParticipantId())) {
            wrapper.eq(ReplenishmentPO::getParticipantId, replenishmentDO.getParticipantId());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getFirmId())) {
            wrapper.eq(ReplenishmentPO::getFirmId, replenishmentDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getOrderNo())) {
            wrapper.eq(ReplenishmentPO::getOrderNo, replenishmentDO.getOrderNo());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getGmtLastRs())) {
            wrapper.eq(ReplenishmentPO::getGmtLastRs, replenishmentDO.getGmtLastRs());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getDeviceName())) {
            wrapper.like(ReplenishmentPO::getDeviceName, replenishmentDO.getDeviceName().trim());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getDeviceNameOrId())) {
            wrapper.like(ReplenishmentPO::getDeviceId, replenishmentDO.getDeviceNameOrId().trim())
                    .or().like(ReplenishmentPO::getDeviceName, replenishmentDO.getDeviceNameOrId().trim());

        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(replenishmentDO.getGmtCreatedEnd())) {
            wrapper.between(ReplenishmentPO::getGmtCreated, replenishmentDO.getGmtCreatedStart(), replenishmentDO.getGmtCreatedEnd());
        }
        wrapper.ge(replenishmentDO.getGmtCreatedStart() != null, ReplenishmentPO::getGmtCreated, replenishmentDO.getGmtCreatedStart());
        return wrapper;
    }

    @Override
    public long count(ReplenishmentDO replenishmentDO) {
        return getWrapper(replenishmentDO).count();
    }

    @Override
    public void updateReplenishment(ReplenishmentDO replenishmentDO) {

        LambdaUpdateChainWrapper<ReplenishmentPO> wrapper = lambdaUpdate();
        if (!CollectionUtils.isEmpty(replenishmentDO.getRsSnList())){
            wrapper.in(ReplenishmentPO::getRsSn, replenishmentDO.getRsSnList());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getRsSn())){
            wrapper.eq(ReplenishmentPO::getRsSn, replenishmentDO.getRsSn());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getOrderNo())){
            wrapper.set(ReplenishmentPO::getOrderNo, replenishmentDO.getOrderNo());
        }
        if (!ObjectUtils.isEmpty(replenishmentDO.getRsStatus())){
            wrapper.set(ReplenishmentPO::getRsStatus, replenishmentDO.getRsStatus());
        }
        wrapper.update();

    }
}
